﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Collections;

namespace ChungNhanSinhVien.Infrastructures
{
    public class SharedFunc
    {
        public string CreateMD5Hash(string input)
        {

            // Use input string to calculate MD5 hash

            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes;
            byte[] hashBytes;
            if (input != null)
            {
                inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
                hashBytes = md5.ComputeHash(inputBytes);



                // Convert the byte array to hexadecimal string

                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < hashBytes.Length; i++)
                {

                    sb.Append(hashBytes[i].ToString("x2"));

                    // To force the hex string to lower-case letters instead of

                    // upper-case, use he following line instead:

                    // sb.Append(hashBytes[i].ToString("x2"));

                }

                return sb.ToString();
            }
            return "";
        }


        public DataTable GetDataExcel(OleDbConnection oleDbConnection, string selectSheet)
        {
            DataTable dataTable = null;
            OleDbCommand oleDbCommand = new OleDbCommand();
            oleDbCommand.Connection = oleDbConnection;
            oleDbCommand.CommandText = string.Format("Select * from [{0}]", selectSheet);
            OleDbDataAdapter oleDbAdapter = new OleDbDataAdapter();
            oleDbAdapter.SelectCommand = oleDbCommand;
            dataTable = new DataTable();
            oleDbAdapter.Fill(dataTable);
            return dataTable;
        }

        public DataTable removeDataDupExcel(DataTable dt, String[] colPKName)
        {
            if (colPKName == null)
            {
                return dt;
            }

            // Xóa những dòng dữ liệu trùng ở DataTable (có dữ liệu Excel)
            ArrayList listToDelete = new ArrayList();
            ArrayList listDuplicate = new ArrayList();
            foreach (DataRow row in dt.Rows)
            {

                String valueToAdd = "";
                for (int i = 0; i < colPKName.Length; i++)
                {
                    String temp = (i == colPKName.Length - 1) ? "" : "#";
                    valueToAdd += row[colPKName[i]] + temp;
                }

                if (listDuplicate.Contains(valueToAdd))
                {
                    listToDelete.Add(row);

                }
                else
                {
                    listDuplicate.Add(valueToAdd);
                }
            }

            foreach (DataRow row in listToDelete)
            {
                dt.Rows.Remove(row);
            }
            return dt;
        }
    }
}